//搜索条件
var country = false;
var province = false;
var city = false;
var type = false;
// 下拉框对象
var hgs1 = {};
var hgs2 = {};

$(function() {
    topOrsearch()
    $('#icon-remove-city').click(function() {
        country = false;
        province = false;
        city = false;
        $('#city').html('国家/省/市');
        topOrsearch();
    });
    $('#icon-remove-type').click(function() {
        type = false;
        $('#type').html('类型');
        topOrsearch();
    });


    $('#city').click(function() {
        chooseCityParam();
    });
    $('#type').click(function() {
        chooseTypeParam();
    });
})

function topOrsearch() {
    if (country === false && province === false && city === false && type === false) {
        getTopArticle();
    } else {
        getSearchArticle();
    }
}

//获取初始置顶文章
function getTopArticle() {
    $('.search-result').hide();
    $('.search-article').hide();
    $('.top-article').show();
    $('.link').show();
    $('.top-article').html('');
    $('.load').show();
    $.ajax({
        type: "post",
        url: "Api/Article/getTopArticle",
        data: {
            article_type_id: $('#article_type_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            $('.load').hide();
            if (data.status) {
                var t = JSON.parse(data.v);
                $.each(t, function(k, v) {
                    if(v.content&&v.content!=''){
                        $('.top-article-demo a').attr('href', getRootPath() + '/Home/Article/articleInfo?article=' + v.id + '&park_id=' + $('#park_id').val());
                    }
                    else{
                        $('.top-article-demo a').attr('href', 'javascript:void(0)');
                    }
                    $('.top-article-demo img').attr('src', v.img).attr('data-src', v.img);
                    $('.top-article-demo .top-article-item-title').html(sub_str(v.title, 20));
                    $('.top-article').append($('.top-article-demo').html());
                });
                lazyLoad();
                $(window).scroll();
            } else {

            }
        }
    });
}




function getSearchArticle() {
    $('.link').hide();
    $('.top-article').hide();
    $('.search-article').show();
    $('.search-article').html('');

    //搜索结果
    $('.search-result lable').eq(0).html('?');
    var result = [];
    if (city) {
        result.push(city);
    }
    if (type) {
        result.push(type);
    }
    $('.search-result lable').eq(1).html(result.join(','));
    $('.search-result').show();
    $('.load').show();
    $.ajax({
        type: "post",
        url: "Api/Article/getSearchArticle",
        data: {
            article_type_id: $('#article_type_id').val(),
            // country: country,
            // province: province,
            city: city,
            type: type
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                $('.load').hide();
                var t = JSON.parse(data.v);
                $('.search-result lable').eq(0).html(t.length);
                $.each(t, function(k, v) {
                    if (v.css_type == 1) {
                        if(v.content&&v.content!=''){
                            $('.search-article-demo-1 a').attr('href', getRootPath() + '/Home/Article/articleInfo?article=' + v.id + '&park_id=' + $('#park_id').val());
                        }
                        else{
                            $('.search-article-demo-1 a').attr('href', 'javascript:void(0)');
                        }
                        $('.search-article-demo-1 img').attr('src', v.img).attr('data-src', v.img);
                        $('.search-article-demo-1 .search-article-item-1-title').html(sub_str(v.title, 20));
                        $('.search-article-demo-1 .search-article-item-1-introduction').html(sub_str(v.introduction, 20));
                        $('.search-article').append($('.search-article-demo-1').html());
                    } else if (v.css_type == 2) {
                        if(v.content&&v.content!=''){
                            $('.search-article-demo-2 a').attr('href', getRootPath() + '/Home/Article/articleInfo?article=' + v.id + '&park_id=' + $('#park_id').val());
                        }
                        else{
                            $('.search-article-demo-2 a').attr('href', 'javascript:void(0)');
                        }
                        $('.search-article-demo-2 img').attr('src', v.img).attr('data-src', v.img);
                        $('.search-article-demo-2 .search-article-item-2-title').html(sub_str(v.title, 35));
                        $('.search-article').append($('.search-article-demo-2').html());
                    } else if (v.css_type == 3) {
                        if(v.content&&v.content!=''){
                            $('.search-article-demo-3 a').attr('href', getRootPath() + '/Home/Article/articleInfo?article=' + v.id + '&park_id=' + $('#park_id').val());
                        }
                        else{
                            $('.search-article-demo-3 a').attr('href', 'javascript:void(0)');
                        }
                        $('.search-article-demo-3 a').html(sub_str(v.title, 20));
                        $('.search-article').append($('.search-article-demo-3').html());
                    }
                });
                lazyLoad();
                $(window).scroll();
            } else {

            }
        }
    });
}


function chooseCityParam() {
    // $.ajax({
    //     type: "post",
    //     url: "Api/Article/getAddressData",
    //     data: {
    //         article_type_id: $('#article_type_id').val(),
    //         search_value: '无',
    //         search_field: '',
    //         get_field: 'country'
    //     },
    //     async: true,
    //     dataType: 'json',
    //     success: function(data) {
    //         if (data.status) {
    //             var t1 = JSON.parse(data.v);
    //             hgs1 = new selectSwiper({
    //                 el: '.select-box1',
    //                 mustSelect: true, // 是否必选，默认false
    //                 data: t1,
    //                 init: function(index) {},
    //                 okFunUndefind: function() { //选择空执行
    //                     errorAlert('请选择');
    //                     return false;
    //                 },
    //                 okFun: function(index) { //确认按钮执行
    //                     country = this.data[index];
                        
    //                 }
    //             });
    //             hgs1.openSelectSwiper();
    //         } else {
    //             errorAlert(data.msg);
    //         }
    //     }
    // });
    $.ajax({
        type: "post",
        url: "Api/Article/getAddressData",
        data: {
            article_type_id: $('#article_type_id').val(),
            search_value: '无',
            search_field: '',
            get_field: 'city'
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t3 = JSON.parse(data.v);
                hgs1 = new selectSwiper({
                    el: '.select-box1',
                    mustSelect: true, // 是否必选，默认false
                    data: t3,
                    init: function(index) {},
                    okFunUndefind: function() { //选择空执行
                        city = false;
                        //20191012加入新需求，搜索选择结果要放到框框里
                        $('#city').html('全部');
                        getSearchArticle();
                        hgs1.closeSelectSwiper();
                    },
                    okFun: function(index) { //确认按钮执行
                        city = this.data[index];
                        //20191012加入新需求，搜索选择结果要放到框框里
                        $('#city').html(city);
                        getSearchArticle();
                    }
                });
                hgs1.openSelectSwiper();
            } else {
                errorAlert(data.msg);
            }
        }
    });
}


function chooseTypeParam() {
    $.ajax({
        type: "post",
        url: "Api/Article/getTypeData",
        data: {
            article_type_id: $('#article_type_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                hgs2 = new selectSwiper({
                    el: '.select-box2',
                    mustSelect: true, // 是否必选，默认false
                    data: t,
                    init: function(index) {},
                    okFunUndefind: function() { //选择空执行
                        type = false;
                        $('#type').html('全部');
                        getSearchArticle();
                        hgs2.closeSelectSwiper();
                    },
                    okFun: function(index) { //确认按钮执行
                        type = this.data[index];
                        $('#type').html(type);
                        getSearchArticle();
                    }
                });
                hgs2.openSelectSwiper();
            } else {
                errorAlert(data.msg);
            }
        }
    });
}